package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //查询用户列表
    @Override
    public PageResult<User> list(UserDto userDto) {
        //设置分页参数
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        //调用mapper查询分页数据
        List<User> userList = userMapper.selectList(userDto);
        //解析并封装分页结果
        Page<User> pageInfo=(Page<User>) userList;
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getResult());
    }

    //添加用户
    @Override
    public void add(User user) {
        //补全基础属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        String password =user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.insert(user);
    }

    //批量删除用户
    @Override
    public void deleteByIds(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }

    //根据id查询用户
    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    //更新用户
    @Override
    public void update(User user) {
        //补全基本属性
        user.setUpdateTime(LocalDateTime.now());
        //更新密码
        String newPassword=user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes()));
        userMapper.update(user);
    }

    //根据角色查询用户
    @Override
    public List<User> getByRole(String roleLabel) {
        return userMapper.getByRole(roleLabel);
    }

    //根据部门查询用户
    @Override
    public List<User> getByDeptId(Integer deptId) {
        return userMapper.getByDeptId(deptId);
    }

    @Override
    public LoginResultVo login(User user) {
        //调用mapper 根据用户名查询用户
        User u = userMapper.getByUsername(user.getUsername());
        //判断用户是否存在
        if(u==null){
            log.info("用户不存在");
            throw new BusinessException("用户或密码错误");
        }
        //密码是否正确
        String pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!u.getPassword().equals( pass)){
            log.info("密码错误");
            throw new BusinessException("用户或密码错误");
        }
        //判断状态
        if(u.getStatus()==0){
            log.info("该用户已停用");
            throw new BusinessException("该用户已停用");
        }
        //封装结果并返回
        LoginResultVo loginResult = new LoginResultVo();
        loginResult.setId(u.getId());
        loginResult.setUsername(u.getUsername());
        loginResult.setName(u.getName());
        loginResult.setImage(u.getImage());
        loginResult.setRoleLabel(u.getRoleLabel());

        //调用工具类,生成JWT令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt = JwtUtils.generateToken(claims);

        loginResult.setToken(jwt);
        return loginResult;
    }
}
